<?php
// Viser et bilde ned prosentvis dekning av 'noe'
// getimage.php?w=200&h=50&l=30&t=100
// getimage.php?w=100&h=5&l=30&t=100
// Parametre
//  w = bredde i pixler
//  h = høyde i pixler
//  l = antall ledige
//  t = totalt antall.
//  c = 0, 1, or 2 - Color Scheme
//  Hvis ikke c er oppgitt, er følgende standard:
//	Hvit bakgrunn/grønn bar - mer enn 20% ledig
//	lys rød bakgrunn/gul bar - mindre enn 20% ledig
//	rød bar - ingen ledig

header('Content-Type: image/png');
$width = 600;
$height = 200;
$ledige = 50;
$total  = 100;
$colscheme  = 0;
$autoColscheme = 1;

if(isset($_GET['c'])) {
	$colscheme = $_GET['c'];
	$autoColscheme = 0;
}

if(isset($_GET['w'])) {
	$width = $_GET['w'];
}

if(isset($_GET['h'])) {
	$height = $_GET['h'];
}

if(isset($_GET['l'])) {
	$ledige = $_GET['l'];
}

if(isset($_GET['t'])) {
	$total = $_GET['t'];
}

if($autoColscheme == 1) {
	$colscheme = 0;
	if($ledige==0) {
		$colscheme = 2;
	} else
	if(($ledige*100)/$total < 20) {
		$colscheme = 1;
	}
}

$image = @imagecreate($width, $height)
    or die("Cannot Initialize new GD image stream");

// allocate all required colors
$colorRed          = imageColorAllocate($image, 0, 255, 0);
switch($colscheme) {
	case 0:
		$colorBackgr       = imageColorAllocate($image, 255,255,255);
		$colorGrid         = imageColorAllocate($image, 0, 0, 0);
		$colorBar          = imageColorAllocate($image, 0, 255, 0);
		break;
	case 1:
		$colorBackgr       = imageColorAllocate($image, 255,128,128);
		$colorGrid         = imageColorAllocate($image, 0, 0, 0);
		$colorBar          = imageColorAllocate($image, 255, 255, 0);
		break;
	case 2:
		$colorBackgr       = imageColorAllocate($image, 255,0,0);
		$colorGrid         = imageColorAllocate($image, 0, 0, 0);
		$colorBar          = imageColorAllocate($image, 255, 0, 0);
		break;
default:
		$colorBackgr       = imageColorAllocate($image, 255,255,255);
		$colorGrid         = imageColorAllocate($image, 0, 0, 0);
		$colorBar          = imageColorAllocate($image, 0, 255, 0);
		break;
}

// clear the image space with the background color
imageFilledRectangle($image, 0, 0, $width - 1, $height - 1, $colorBackgr);

// Ramme
imageLine($image, 0, 0, $width-1,0, $colorGrid);
imageLine($image, $width-1, 0, $width-1,$height-1, $colorGrid);
imageLine($image, $width-1, $height-1, 0,$height-1, $colorGrid);
imageLine($image, 0,$height-1, 0,0, $colorGrid);

// Grønn
$faktor = ($width-2)/$total;
$pixler = $ledige * $faktor;
if($pixler>0) {
	imageFilledRectangle($image, 1, 1, $pixler, $height - 2, $colorBar);
}

imagePNG($image);

?>